package org.ddmf.frame.service;

import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbSession;

import org.springframework.stereotype.Controller;


/**
 * 框架服務
 * @author JXYEHX
 *
 */
@Controller 
public class AcenetFrameServiceImpl implements AcenetFrameService{

	
	// ####################################################################
	// Service
	// ####################################################################

	private String addomain;
	
	/**
	 * AD網路登入檢核
	 * @param username 登入帳號
	 * @param password 登入密碼
	 * @return
	 */
	public boolean verifyNtlmAccount(String username, String password) {
		try {
			UniAddress mydomaincontroller = UniAddress.getByName(addomain, true);
			NtlmPasswordAuthentication mycreds = new NtlmPasswordAuthentication(addomain, username, password );
			SmbSession.logon( mydomaincontroller, mycreds );
		} catch (UnknownHostException e) {
			//System.out.println("UnknownHostException -> " + e);
			return false;
		} catch (SmbException e) {
			//System.out.println("SmbException -> " + e);
			return false;
		}
			return true;
	}

	public Connection getConn(){
		Connection conn = null;
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
		String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/test/Database1.accdb";
		 conn = DriverManager.getConnection(url, "", "");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	// ####################################################################
	// getter setter
	// ####################################################################	
	
	public String getAddomain() {
		return addomain;
	}

	public void setAddomain(String addomain) {
		this.addomain = addomain;
	}

	
}
